

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="hz">
  <meta name="keywords" content="">
  
    <meta name="description" content="因为项目使用的uni-app来做小程序，所以，不得不去研究下uni-app对小程序打包的整个过程，众所周知，最简单的办法是去github官网去看看他的readme文件，介绍，无奈没发现什么有价值的介绍，只能自己单步调试一下打包的过程了。 step1、在vscode中配置调试启动配置在launch.json中添加这一项就ok，这样就可以已调试的方式启动  yarn 命令，我这里是start。 123">
<meta property="og:type" content="article">
<meta property="og:title" content="浅析Uni-App的加载过程">
<meta property="og:url" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/index.html">
<meta property="og:site_name" content="心随我动">
<meta property="og:description" content="因为项目使用的uni-app来做小程序，所以，不得不去研究下uni-app对小程序打包的整个过程，众所周知，最简单的办法是去github官网去看看他的readme文件，介绍，无奈没发现什么有价值的介绍，只能自己单步调试一下打包的过程了。 step1、在vscode中配置调试启动配置在launch.json中添加这一项就ok，这样就可以已调试的方式启动  yarn 命令，我这里是start。 123">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307185142192.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307190611229.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307190027189.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307193648564.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307194446009.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307213703832.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307214847221.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307220428348.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307221717949.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307225158705.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307230528416.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307230626272.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307232816427.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220308000801898.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220308005120412.png">
<meta property="article:published_time" content="2022-03-07T10:43:04.000Z">
<meta property="article:modified_time" content="2022-11-28T01:25:08.269Z">
<meta property="article:author" content="hz">
<meta property="article:tag" content="小程序">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://blog.brzhang.club/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307185142192.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>浅析Uni-App的加载过程 - 心随我动</title><meta name="robots" content="noindex">

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"blog.brzhang.club","root":"/","version":"1.9.5","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 30vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Brzhang</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="浅析Uni-App的加载过程"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        hz
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-03-07 18:43" pubdate>
          2022年3月7日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          13k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          112 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">浅析Uni-App的加载过程</h1>
            
            
              <div class="markdown-body">
                
                <p>因为项目使用的<a target="_blank" rel="noopener" href="https://github.com/dcloudio/uni-app">uni-app</a>来做小程序，所以，不得不去研究下uni-app对小程序打包的整个过程，众所周知，最简单的办法是去github官网去看看他的readme文件，介绍，无奈没发现什么有价值的介绍，只能自己单步调试一下打包的过程了。</p>
<h3 id="step1、在vscode中配置调试启动配置"><a href="#step1、在vscode中配置调试启动配置" class="headerlink" title="step1、在vscode中配置调试启动配置"></a>step1、在vscode中配置调试启动配置</h3><p>在launch.json中添加这一项就ok，这样就可以已调试的方式启动  yarn 命令，我这里是start。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-comment">// A launch configuration that compiles the extension and then opens it inside a new window</span><br><span class="hljs-comment">// Use IntelliSense to learn about possible attributes.</span><br><span class="hljs-comment">// Hover to view descriptions of existing attributes.</span><br><span class="hljs-comment">// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387</span><br><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;version&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;0.2.0&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;configurations&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>		<span class="hljs-punctuation">&#123;</span><br>      <span class="hljs-attr">&quot;type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;node&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;request&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;launch&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;yarn&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;runtimeExecutable&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;yarn&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;runtimeArgs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>        <span class="hljs-string">&quot;start&quot;</span><br>      <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;port&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5858</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;cwd&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;$&#123;workspaceRoot&#125;&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;timeout&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">10000</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>		....<br><span class="hljs-punctuation">&#125;</span><br><br></code></pre></td></tr></table></figure>

<p>然后就是启动调试了，当然可以在适当的位置先配置一个断点，比如在这个文件中</p>
<p><code>./node_modules/@dcloudio/vue-cli-plugin-uni/lib/env.js</code>增加一个断点</p>
<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307185142192.png" srcset="/img/loading.gif" lazyload alt="image-20220307185142192" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307185142192.png"></p>
<p>断掉断到之后，可以发现，栈底是在执行这个文件的代码<code>/internal/main/run_main_module.js</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-meta">&#x27;use strict&#x27;</span>;<br><br><span class="hljs-keyword">const</span> &#123;<br>  prepareMainThreadExecution<br>&#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;internal/bootstrap/pre_execution&#x27;</span>);<br><br><span class="hljs-title function_">prepareMainThreadExecution</span>(<span class="hljs-literal">true</span>);<br><br><span class="hljs-keyword">const</span> <span class="hljs-title class_">CJSModule</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;internal/modules/cjs/loader&#x27;</span>);<br><br><span class="hljs-title function_">markBootstrapComplete</span>();<br><br><span class="hljs-comment">// Note: this actually tries to run the module as a ESM first if</span><br><span class="hljs-comment">// --experimental-modules is on.</span><br><span class="hljs-comment">// TODO(joyeecheung): can we move that logic to here? Note that this</span><br><span class="hljs-comment">// is an undocumented method available via `require(&#x27;module&#x27;).runMain`</span><br><span class="hljs-title class_">CJSModule</span>.<span class="hljs-title function_">runMain</span>();<br></code></pre></td></tr></table></figure>

<p>所以，我们发现了一个点，整个构建的开始就是从CJSModule.runMain();这句话开始的。</p>
<h3 id="step2、分析uni的打包过程中加载了哪些文件"><a href="#step2、分析uni的打包过程中加载了哪些文件" class="headerlink" title="step2、分析uni的打包过程中加载了哪些文件"></a>step2、分析uni的打包过程中加载了哪些文件</h3><p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307190611229.png" srcset="/img/loading.gif" lazyload alt="image-20220307190611229" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307190611229.png"></p>
<p>这里应该就是解析参数，准备开始执行yarn命令了，我们看一看我们的yarn命令，</p>
<p><code>UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch</code>，所以，第一个加载的模块应该就是</p>
<p>@vue&#x2F;cli-service&#x2F;bin&#x2F;vue-cli-service.js</p>
<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307190027189.png" srcset="/img/loading.gif" lazyload alt="image-20220307190027189" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307190027189.png"></p>
<p>随后的过程记录粗粒度一点点，后面vue-cli-service会加载各种plugin，具体怎么判断是否是一个plugin，有一定的规则命名;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> pluginRE = <span class="hljs-regexp">/^(@vue\/|vue-|@[\w-]+(\.)?[\w-]+\/vue-)cli-plugin-/</span><br><span class="hljs-keyword">const</span> scopeRE = <span class="hljs-regexp">/^@[\w-]+(\.)?[\w-]+\//</span><br><span class="hljs-keyword">const</span> officialRE = <span class="hljs-regexp">/^@vue\//</span><br><br><span class="hljs-keyword">const</span> officialPlugins = [<br>  <span class="hljs-string">&#x27;babel&#x27;</span>,<br>  <span class="hljs-string">&#x27;e2e-cypress&#x27;</span>,<br>  <span class="hljs-string">&#x27;e2e-nightwatch&#x27;</span>,<br>  <span class="hljs-string">&#x27;e2e-webdriverio&#x27;</span>,<br>  <span class="hljs-string">&#x27;eslint&#x27;</span>,<br>  <span class="hljs-string">&#x27;pwa&#x27;</span>,<br>  <span class="hljs-string">&#x27;router&#x27;</span>,<br>  <span class="hljs-string">&#x27;typescript&#x27;</span>,<br>  <span class="hljs-string">&#x27;unit-jest&#x27;</span>,<br>  <span class="hljs-string">&#x27;unit-mocha&#x27;</span>,<br>  <span class="hljs-string">&#x27;vuex&#x27;</span><br>]<br><span class="hljs-built_in">exports</span>.<span class="hljs-property">isPlugin</span> = <span class="hljs-function"><span class="hljs-params">id</span> =&gt;</span> pluginRE.<span class="hljs-title function_">test</span>(id)<br></code></pre></td></tr></table></figure>

<p>然后从项目的package.json的依赖中解析出来了三个需要加载的插件</p>
<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307193648564.png" srcset="/img/loading.gif" lazyload alt="image-20220307193648564" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307193648564.png"></p>
<p>很显然第一个加载并执行的插件就是这个vue-cli-plugin-hbuilderx插件。接着依次去加载其他两个，我们先看第一个插件吧。</p>
<h4 id="加载vue-cli-plugin-uni插件"><a href="#加载vue-cli-plugin-uni插件" class="headerlink" title="加载vue-cli-plugin-uni插件"></a>加载vue-cli-plugin-uni插件</h4><p>进入一看，他直接退出了，原因是只处理 app-plus相关的逻辑，而我们编译的是mp-weixin</p>
<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307194446009.png" srcset="/img/loading.gif" lazyload loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307194446009.png"></p>
<p>所以，忽略，直接进入下面一个 vue-cli-plugin-uni的加载，这个文件先加载了一个.env.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 初始化全局插件对象</span><br><span class="hljs-variable language_">global</span>.<span class="hljs-property">uniPlugin</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@dcloudio/uni-cli-shared/lib/plugin&#x27;</span>).<span class="hljs-title function_">init</span>()<br></code></pre></td></tr></table></figure>

<p>这个里面就初始化了全局插件对象，然后它的init操作中去加载了对应平台的插件，也就是说，编译什么平台就加载什么平台的插件。</p>
<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307213703832.png" srcset="/img/loading.gif" lazyload alt="image-20220307213703832" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307213703832.png"></p>
<p>我们编译的是小程序的，所以加载的是@dcloudio&#x2F;uni-mp-weixin。</p>
<p>这里在外后走就是初始化预处理器，<strong>然后发现Plugin.platforms中居然有一个h5，暂时不知道为什么编译小程序平台需要h5，难道h5是必须的嘛</strong>？这个问题先放一放</p>
<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307214847221.png" srcset="/img/loading.gif" lazyload alt="image-20220307214847221" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307214847221.png"></p>
<p>知道env.js执行完，就可以知道，他做了一下几件事情：</p>
<ol>
<li>初始化了全局插件，读取了一些webpack配置，特别是copyWebpackOptions</li>
<li>以及一些全局的options，如cssVars，extNames，subPackages等<ol>
<li><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307220428348.png" srcset="/img/loading.gif" lazyload alt="image-20220307220428348" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307220428348.png"></li>
</ol>
</li>
<li>设置了一堆的环境变量<ol>
<li>process.env.UNI_PLATFORM</li>
<li>process.env.VUE_APP_PLATFORM</li>
<li>process.env.UNI_OUTPUT_DIR</li>
</ol>
</li>
<li>确定了build后产物的路径process.env.UNI_OUTPUT_DIR</li>
<li>对分包subPackages的处理</li>
<li>process.env.VUE_APP_DEBUG 开关</li>
<li>增加了一堆的hack操作，把vue相关的库指向了uni自己的包装过的<ol>
<li><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307221717949.png" srcset="/img/loading.gif" lazyload alt="image-20220307221717949" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307221717949.png"></li>
</ol>
</li>
</ol>
<p>插件加载之后，下一步 就是要执行插件，执行操作设计到对vue代码转换为小程序语法的过程，这部分内容，我觉得还是单独开一篇比较合适。</p>
<h4 id="加载vue-cli-plugin-uni-optimize插件"><a href="#加载vue-cli-plugin-uni-optimize插件" class="headerlink" title="加载vue-cli-plugin-uni-optimize插件"></a>加载vue-cli-plugin-uni-optimize插件</h4><p>这个插件主要是做了一些代码优化，代码也不是很多，很直观就可以看明白</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;path&#x27;</span>)<br><br><span class="hljs-keyword">const</span> webpack = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;webpack&#x27;</span>)<br><br><span class="hljs-keyword">const</span> <span class="hljs-title class_">WebpackOptimizePlugin</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;./packages/webpack-optimize-plugin&#x27;</span>)<br><br><span class="hljs-keyword">const</span> &#123;<br>  src,<br>  lib<br>&#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@dcloudio/uni-h5/path&#x27;</span>)<br><br><span class="hljs-keyword">const</span> <span class="hljs-title function_">resolve</span> = dir =&gt; path.<span class="hljs-title function_">resolve</span>(__dirname, <span class="hljs-string">&#x27;./&#x27;</span>, dir)<br><br><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-function">(<span class="hljs-params">api, options</span>) =&gt;</span> &#123;<br>  <span class="hljs-keyword">if</span> (!process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_OPT_TREESHAKINGNG</span>) &#123;<br>    <span class="hljs-keyword">return</span><br>  &#125;<br><br>  <span class="hljs-keyword">if</span> (process.<span class="hljs-property">env</span>.<span class="hljs-property">NODE_ENV</span> !== <span class="hljs-string">&#x27;production&#x27;</span> || process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_PLATFORM</span> !== <span class="hljs-string">&#x27;h5&#x27;</span>) &#123;<br>    <span class="hljs-keyword">return</span><br>  &#125;<br><br>  <span class="hljs-comment">// 组件</span><br>  <span class="hljs-keyword">const</span> uniComponentsPath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;.tmp/components.js&#x27;</span>)<br><br>  <span class="hljs-keyword">const</span> uniInvokeApiPath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;.tmp/invoke-api.js&#x27;</span>)<br>  <span class="hljs-keyword">const</span> uniServiceApiPath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;.tmp/api.js&#x27;</span>)<br>  <span class="hljs-keyword">const</span> uniApiProtocolPath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;.tmp/protocol.js&#x27;</span>)<br>  <span class="hljs-keyword">const</span> uniApiSubscribePath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;.tmp/subscribe.js&#x27;</span>)<br>  <span class="hljs-keyword">const</span> uniH5AppComponentsPath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;.tmp/app-components.js&#x27;</span>)<br>  <span class="hljs-keyword">const</span> uniH5AppMixinsPath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;.tmp/app-mixins.js&#x27;</span>)<br>  <span class="hljs-keyword">const</span> uniH5SystemRoutes = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">&#x27;.tmp/system-routes.js&#x27;</span>)<br><br>  options.<span class="hljs-property">transpileDependencies</span>.<span class="hljs-title function_">push</span>(<span class="hljs-regexp">/vue-cli-plugin-uni-optimize/</span>)<br>  options.<span class="hljs-property">transpileDependencies</span>.<span class="hljs-title function_">push</span>(<span class="hljs-regexp">/uni-h5/</span>)<br><br>  api.<span class="hljs-title function_">configureWebpack</span>(<span class="hljs-function"><span class="hljs-params">webpackConfig</span> =&gt;</span> &#123;<br>    <span class="hljs-keyword">return</span> &#123;<br>      <span class="hljs-attr">watch</span>: <span class="hljs-literal">true</span>,<br>      <span class="hljs-attr">resolve</span>: &#123;<br>        <span class="hljs-attr">alias</span>: &#123;<br>          [<span class="hljs-string">&#x27;uni-&#x27;</span> + process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_PLATFORM</span>]: path.<span class="hljs-title function_">join</span>(lib, <span class="hljs-string">`<span class="hljs-subst">$&#123;process.env.UNI_PLATFORM&#125;</span>/main.js`</span>),<br>          <span class="hljs-string">&#x27;uni-core&#x27;</span>: path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;core&#x27;</span>),<br>          <span class="hljs-string">&#x27;uni-view&#x27;</span>: path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;core/view&#x27;</span>),<br>          <span class="hljs-string">&#x27;uni-service&#x27;</span>: path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;core/service&#x27;</span>),<br>          <span class="hljs-string">&#x27;uni-shared&#x27;</span>: path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;shared&#x27;</span>),<br>          <span class="hljs-string">&#x27;uni-mixins&#x27;</span>: path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;core/view/mixins&#x27;</span>),<br>          <span class="hljs-string">&#x27;uni-helpers&#x27;</span>: path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;core/helpers&#x27;</span>),<br>          <span class="hljs-string">&#x27;uni-platform&#x27;</span>: path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;platforms/&#x27;</span> + process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_PLATFORM</span>),<br><br>          <span class="hljs-comment">// tree shaking</span><br>          <span class="hljs-string">&#x27;uni-components&#x27;</span>: uniComponentsPath,<br>          <span class="hljs-string">&#x27;uni-invoke-api&#x27;</span>: uniInvokeApiPath,<br>          <span class="hljs-string">&#x27;uni-service-api&#x27;</span>: uniServiceApiPath,<br>          <span class="hljs-string">&#x27;uni-api-protocol&#x27;</span>: uniApiProtocolPath,<br>          <span class="hljs-string">&#x27;uni-api-subscribe&#x27;</span>: uniApiSubscribePath,<br>          <span class="hljs-comment">// h5 components</span><br>          <span class="hljs-string">&#x27;uni-h5-app-components&#x27;</span>: uniH5AppComponentsPath,<br>          <span class="hljs-string">&#x27;uni-h5-app-mixins&#x27;</span>: uniH5AppMixinsPath,<br>          <span class="hljs-string">&#x27;uni-h5-system-routes&#x27;</span>: uniH5SystemRoutes<br>        &#125;<br>      &#125;,<br>      <span class="hljs-attr">plugins</span>: [<br>        <span class="hljs-keyword">new</span> <span class="hljs-title class_">WebpackOptimizePlugin</span>(),<br>        <span class="hljs-keyword">new</span> webpack.<span class="hljs-title class_">DefinePlugin</span>(&#123;<br>          <span class="hljs-attr">__VERSION__</span>: <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(<span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@dcloudio/uni-&#x27;</span> + process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_PLATFORM</span> + <span class="hljs-string">&#x27;/package.json&#x27;</span>).<span class="hljs-property">version</span>),<br>          <span class="hljs-attr">__PLATFORM__</span>: <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_PLATFORM</span>)<br>        &#125;),<br>        <span class="hljs-keyword">new</span> webpack.<span class="hljs-title class_">ProvidePlugin</span>(&#123;<br>          <span class="hljs-attr">console</span>: [path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;core/helpers/console&#x27;</span>), <span class="hljs-string">&#x27;default&#x27;</span>],<br>          <span class="hljs-title class_">UniViewJSBridge</span>: [path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;core/view/bridge/index&#x27;</span>)],<br>          <span class="hljs-title class_">UniServiceJSBridge</span>: [path.<span class="hljs-title function_">join</span>(src, <span class="hljs-string">&#x27;core/service/bridge/index&#x27;</span>)]<br>        &#125;)<br>      ]<br>    &#125;<br>  &#125;)<br><br>  api.<span class="hljs-title function_">chainWebpack</span>(<span class="hljs-function"><span class="hljs-params">webpackConfig</span> =&gt;</span> &#123;<br>    <span class="hljs-comment">// Vue</span><br>    webpackConfig.<span class="hljs-property">module</span><br>      .<span class="hljs-title function_">rule</span>(<span class="hljs-string">&#x27;vue&#x27;</span>)<br>      .<span class="hljs-title function_">use</span>(<span class="hljs-string">&#x27;vue-loader&#x27;</span>)<br>      .<span class="hljs-title function_">tap</span>(<span class="hljs-function"><span class="hljs-params">options</span> =&gt;</span> <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">assign</span>(options, &#123;<br>        <span class="hljs-attr">isH5TreeShaking</span>: <span class="hljs-literal">true</span>,<br>        <span class="hljs-attr">cacheDirectory</span>: <span class="hljs-literal">false</span>,<br>        <span class="hljs-attr">cacheIdentifier</span>: <span class="hljs-literal">false</span><br>      &#125;))<br>      .<span class="hljs-title function_">end</span>()<br>      .<span class="hljs-property">uses</span><br>      .<span class="hljs-title function_">delete</span>(<span class="hljs-string">&#x27;cache-loader&#x27;</span>)<br>  &#125;)<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p>从这里也了解了，为啥编译一个小程序还需要h5的插件,然来是vue-cli-plugin-uni-optimize这个插件需要基于h5</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123;<br>  src,<br>  lib<br>&#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@dcloudio/uni-h5/path&#x27;</span>)<br></code></pre></td></tr></table></figure>

<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307225158705.png" srcset="/img/loading.gif" lazyload alt="image-20220307225158705" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307225158705.png"></p>
<p>用到了他里面的一些核心能力，这也从侧面了解，uni编译各种版本，基本上都会最终依赖上这个h5的插件。这里也可以了解到，如果是开发模式，这个插件不会被执行，因为</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">if</span> (!process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_OPT_TREESHAKINGNG</span>) &#123;<br>    <span class="hljs-keyword">return</span><br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="step3、执行插件"><a href="#step3、执行插件" class="headerlink" title="step3、执行插件"></a>step3、执行插件</h3><p>通常一个插件都会暴露出一个方法</p>
<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307230528416.png" srcset="/img/loading.gif" lazyload alt="image-20220307230528416" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307230528416.png"></p>
<p>这个方法，将会被vue-cli-service去挨个挨个执行</p>
<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307230626272.png" srcset="/img/loading.gif" lazyload alt="image-20220307230626272" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307230626272.png"></p>
<p>插件被执行完之后，最终就会进入到构建环节</p>
<h3 id="step3、进入构建环节"><a href="#step3、进入构建环节" class="headerlink" title="step3、进入构建环节"></a>step3、进入构建环节</h3><p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307232816427.png" srcset="/img/loading.gif" lazyload alt="image-20220307232816427" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220307232816427.png"></p>
<p>这里就不直接展开了，小程序产物构建完全可以单独搞一章了。</p>
<p>这里我们了解整个过程之后，发现uni打开做了这么几件事</p>
<ol>
<li>使用yarn start命令，会直接出发vue-cli-service活动起来，然后开始加载vue-cli相关的插件</li>
<li>其中uni的三个插件会被加载，并依次之心</li>
<li>同时我们发现，在小程序模式下，hbuilderx插件是不会被执行的</li>
<li>然后我们发现optimize插件在develop模式下是不会被执行的</li>
<li>因此我们发现只有vue-cli-plugin-uni这个插件会被执行，这里面做了一些列的准备工作，目的就是为了uni能够有一个编译为小程序的环境，他在执行完插件体之后，最终cli-service会转而调用@dcloudio&#x2F;vue-cli-plugin-uni&#x2F;commands&#x2F;build.js中的build方法，然后build方法在执行的过程中，会到@dcloudio&#x2F;vue-cli-plugin-uni&#x2F;lib&#x2F;mp&#x2F;index.js中执行去手机webpack的一些配置，汇总好继续build的构建。</li>
</ol>
<p><strong>下面附上这些个过程的堆栈信息，其调用过程一目了然。</strong></p>
<blockquote>
<p>webpackConfig (&#x2F;Users&#x2F;xxx&#x2F;mp-project&#x2F;node_modules&#x2F;<strong>@dcloudio&#x2F;vue-cli-plugin-uni&#x2F;lib&#x2F;mp&#x2F;index.js</strong>:149)<br>….<strong>此处省略一些堆栈</strong><br>build (&#x2F;Users&#x2F;xxx&#x2F;mp-project&#x2F;node_modules&#x2F;@dcloudio&#x2F;vue-cli-plugin-uni&#x2F;commands&#x2F;build.js:137)<br><anonymous> (&#x2F;Users&#x2F;xxx&#x2F;mp-project&#x2F;node_modules&#x2F;<strong>@dcloudio&#x2F;vue-cli-plugin-uni&#x2F;commands&#x2F;build.js:62</strong>)<br>run (&#x2F;Users&#x2F;xxx&#x2F;mp-project&#x2F;node_modules&#x2F;@vue&#x2F;cli-service&#x2F;lib&#x2F;<strong>Service.js:230</strong>)<br><anonymous> (&#x2F;Users&#x2F;xxx&#x2F;mp-project&#x2F;node_modules&#x2F;@vue&#x2F;cli-service&#x2F;bin&#x2F;<strong>vue-cli-service.js:36</strong>)<br>Module._compile (<node_internals>&#x2F;internal&#x2F;modules&#x2F;cjs&#x2F;loader.js:956)<br>Module._extensions..js (<node_internals>&#x2F;internal&#x2F;modules&#x2F;cjs&#x2F;loader.js:973)<br>Module.load (<node_internals>&#x2F;internal&#x2F;modules&#x2F;cjs&#x2F;loader.js:812)<br>Module._load (<node_internals>&#x2F;internal&#x2F;modules&#x2F;cjs&#x2F;loader.js:724)<br>Module.runMain (<node_internals>&#x2F;internal&#x2F;modules&#x2F;cjs&#x2F;<strong>loader.js:1025</strong>)<br><anonymous> (<node_internals>&#x2F;internal&#x2F;main&#x2F;<strong>run_main_module.js:17</strong>)</node_internals></anonymous></node_internals></node_internals></node_internals></node_internals></node_internals></anonymous></anonymous></p>
</blockquote>
<p>这里也贴一下vue-cli-plugin-uni&#x2F;lib&#x2F;mp&#x2F;index.js的核心代码，后续的文章就了解这个里面怎么把vue代码构建成小程序的。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = &#123;<br>  <span class="hljs-attr">vueConfig</span>: &#123;<br>    <span class="hljs-attr">parallel</span>: <span class="hljs-literal">false</span><br>  &#125;,<br>  webpackConfig (webpackConfig, vueOptions, api) &#123;<br>    <span class="hljs-keyword">if</span> (!webpackConfig.<span class="hljs-property">optimization</span>) &#123;<br>      webpackConfig.<span class="hljs-property">optimization</span> = &#123;&#125;<br>    &#125;<br>    <span class="hljs-comment">// disable noEmitOnErrors</span><br>    webpackConfig.<span class="hljs-property">optimization</span>.<span class="hljs-property">noEmitOnErrors</span> = <span class="hljs-literal">false</span><br><br>    webpackConfig.<span class="hljs-property">optimization</span>.<span class="hljs-property">runtimeChunk</span> = &#123;<br>      <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;common/runtime&#x27;</span><br>    &#125;<br><br>    webpackConfig.<span class="hljs-property">optimization</span>.<span class="hljs-property">splitChunks</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;../split-chunks&#x27;</span>)()<br><br>    <span class="hljs-title function_">parseEntry</span>()<br><br>    <span class="hljs-keyword">const</span> statCode = process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_USING_STAT</span> ? <span class="hljs-string">&#x27;import \&#x27;@dcloudio/uni-stat\&#x27;;&#x27;</span> : <span class="hljs-string">&#x27;&#x27;</span><br><br>    <span class="hljs-keyword">let</span> beforeCode = <span class="hljs-string">&#x27;import \&#x27;uni-pages\&#x27;;&#x27;</span><br><br>    <span class="hljs-keyword">const</span> plugins = [<br>      <span class="hljs-keyword">new</span> <span class="hljs-title class_">WebpackUniAppPlugin</span>(),<br>      <span class="hljs-title function_">createUniMPPlugin</span>(),<br>      <span class="hljs-keyword">new</span> webpack.<span class="hljs-title class_">ProvidePlugin</span>(<span class="hljs-title function_">getProvides</span>())<br>    ]<br><br>    <span class="hljs-keyword">if</span> ((process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_SUBPACKGE</span> || process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_MP_PLUGIN</span>) &amp;&amp; process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_SUBPACKGE</span> !== <span class="hljs-string">&#x27;main&#x27;</span>) &#123;<br>      plugins.<span class="hljs-title function_">push</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">PreprocessAssetsPlugin</span>())<br>    &#125;<br><br>    <span class="hljs-keyword">if</span> (process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_MP_PLUGIN</span>) &#123;<br>      <span class="hljs-comment">// 小程序插件入口使用</span><br>      <span class="hljs-comment">// packages\webpack-uni-mp-loader\lib\plugin\index-new.js -&gt; addMPPluginRequire</span><br>      beforeCode += <span class="hljs-string">`wx.__webpack_require_<span class="hljs-subst">$&#123;process.env.UNI_MP_PLUGIN.replace(<span class="hljs-string">&#x27;-&#x27;</span>, <span class="hljs-string">&#x27;_&#x27;</span>)&#125;</span>__ = __webpack_require__;`</span><br><br>      <span class="hljs-keyword">const</span> <span class="hljs-variable constant_">UNI_MP_PLUGIN_MAIN</span> = process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_MP_PLUGIN_MAIN</span><br>      <span class="hljs-keyword">if</span> (<span class="hljs-variable constant_">UNI_MP_PLUGIN_MAIN</span>) &#123;<br>        process.<span class="hljs-property">UNI_ENTRY</span>[<span class="hljs-variable constant_">UNI_MP_PLUGIN_MAIN</span>.<span class="hljs-title function_">split</span>(<span class="hljs-string">&#x27;.&#x27;</span>)[<span class="hljs-number">0</span>]] = path.<span class="hljs-title function_">resolve</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_INPUT_DIR</span>, <span class="hljs-variable constant_">UNI_MP_PLUGIN_MAIN</span>)<br>      &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">return</span> &#123;<br>      <span class="hljs-attr">mode</span>: process.<span class="hljs-property">env</span>.<span class="hljs-property">NODE_ENV</span> === <span class="hljs-string">&#x27;production&#x27;</span> ? <span class="hljs-string">&#x27;production&#x27;</span> : <span class="hljs-string">&#x27;development&#x27;</span>,<br>      entry () &#123;<br>        <span class="hljs-keyword">return</span> process.<span class="hljs-property">UNI_ENTRY</span><br>      &#125;,<br>      <span class="hljs-attr">output</span>: &#123;<br>        <span class="hljs-attr">filename</span>: <span class="hljs-string">&#x27;[name].js&#x27;</span>,<br>        <span class="hljs-attr">chunkFilename</span>: <span class="hljs-string">&#x27;[id].js&#x27;</span>,<br>        <span class="hljs-attr">globalObject</span>: process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_PLATFORM</span> === <span class="hljs-string">&#x27;mp-alipay&#x27;</span> ? <span class="hljs-string">&#x27;my&#x27;</span> : <span class="hljs-string">&#x27;global&#x27;</span><br>        <span class="hljs-comment">// sourceMapFilename: &#x27;../.sourcemap/&#x27; + process.env.UNI_PLATFORM + &#x27;/[name].js.map&#x27;</span><br>      &#125;,<br>      <span class="hljs-attr">performance</span>: &#123;<br>        <span class="hljs-attr">hints</span>: <span class="hljs-literal">false</span><br>      &#125;,<br>      <span class="hljs-attr">resolve</span>: &#123;<br>        <span class="hljs-attr">extensions</span>: [<span class="hljs-string">&#x27;.nvue&#x27;</span>],<br>        <span class="hljs-attr">alias</span>: &#123; <span class="hljs-comment">// 仅 mp-weixin</span><br>          <span class="hljs-string">&#x27;mpvue-page-factory&#x27;</span>: <span class="hljs-built_in">require</span>.<span class="hljs-title function_">resolve</span>(<br>            <span class="hljs-string">&#x27;@dcloudio/vue-cli-plugin-uni/packages/mpvue-page-factory&#x27;</span>)<br>        &#125;<br>      &#125;,<br>      <span class="hljs-attr">module</span>: &#123;<br>        <span class="hljs-attr">rules</span>: [&#123;<br>          <span class="hljs-attr">test</span>: path.<span class="hljs-title function_">resolve</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_INPUT_DIR</span>, <span class="hljs-title function_">getMainEntry</span>()),<br>          <span class="hljs-attr">use</span>: [&#123;<br>            <span class="hljs-attr">loader</span>: path.<span class="hljs-title function_">resolve</span>(__dirname, <span class="hljs-string">&#x27;../../packages/wrap-loader&#x27;</span>),<br>            <span class="hljs-attr">options</span>: &#123;<br>              <span class="hljs-attr">before</span>: [<br>                beforeCode + <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;../util&#x27;</span>).<span class="hljs-title function_">getAutomatorCode</span>() + statCode<br>              ]<br>            &#125;<br>          &#125;, &#123;<br>            <span class="hljs-attr">loader</span>: <span class="hljs-string">&#x27;@dcloudio/webpack-uni-mp-loader/lib/main&#x27;</span><br>          &#125;]<br>        &#125;, &#123;<br>          <span class="hljs-attr">resourceQuery</span>: <span class="hljs-regexp">/vue&amp;type=script/</span>,<br>          <span class="hljs-attr">use</span>: [&#123;<br>            <span class="hljs-attr">loader</span>: <span class="hljs-string">&#x27;@dcloudio/webpack-uni-mp-loader/lib/script&#x27;</span><br>          &#125;]<br>        &#125;, &#123;<br>          <span class="hljs-attr">resourceQuery</span>: <span class="hljs-regexp">/vue&amp;type=template/</span>,<br>          <span class="hljs-attr">use</span>: [&#123;<br>            <span class="hljs-attr">loader</span>: <span class="hljs-string">&#x27;@dcloudio/webpack-uni-mp-loader/lib/template&#x27;</span><br>          &#125;, &#123;<br>            <span class="hljs-attr">loader</span>: <span class="hljs-string">&#x27;@dcloudio/vue-cli-plugin-uni/packages/webpack-uni-app-loader/page-meta&#x27;</span><br>          &#125;]<br>        &#125;, <span class="hljs-title function_">createTemplateCacheLoader</span>(api), &#123;<br>          <span class="hljs-attr">resourceQuery</span>: [<br>            <span class="hljs-regexp">/lang=wxs/</span>,<br>            <span class="hljs-regexp">/lang=filter/</span>,<br>            <span class="hljs-regexp">/lang=sjs/</span>,<br>            <span class="hljs-regexp">/blockType=wxs/</span>,<br>            <span class="hljs-regexp">/blockType=filter/</span>,<br>            <span class="hljs-regexp">/blockType=sjs/</span><br>          ],<br>          <span class="hljs-attr">use</span>: [&#123;<br>            <span class="hljs-attr">loader</span>: <span class="hljs-built_in">require</span>.<span class="hljs-title function_">resolve</span>(<br>              <span class="hljs-string">&#x27;@dcloudio/vue-cli-plugin-uni/packages/webpack-uni-filter-loader&#x27;</span>)<br>          &#125;]<br>        &#125;]<br>      &#125;,<br>      plugins<br>    &#125;<br>  &#125;,<br>  chainWebpack (webpackConfig, vueOptions, api) &#123;<br>    <span class="hljs-keyword">if</span> (process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_PLATFORM</span> === <span class="hljs-string">&#x27;mp-baidu&#x27;</span>) &#123;<br>      webpackConfig.<span class="hljs-property">module</span><br>        .<span class="hljs-title function_">rule</span>(<span class="hljs-string">&#x27;js&#x27;</span>)<br>        .<span class="hljs-property">exclude</span><br>        .<span class="hljs-title function_">add</span>(<span class="hljs-regexp">/\.filter\.js$/</span>)<br>    &#125;<br><br>    <span class="hljs-keyword">const</span> compilerOptions = process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_USING_COMPONENTS</span> ? &#123;&#125; : <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;../mp-compiler-options&#x27;</span>)<br><br>    <span class="hljs-title function_">modifyVueLoader</span>(webpackConfig, &#123;&#125;, compilerOptions, api)<br><br>    <span class="hljs-keyword">const</span> styleExt = <span class="hljs-title function_">getPlatformExts</span>().<span class="hljs-property">style</span><br><br>    webpackConfig.<span class="hljs-title function_">plugin</span>(<span class="hljs-string">&#x27;extract-css&#x27;</span>)<br>      .<span class="hljs-title function_">init</span>(<span class="hljs-function">(<span class="hljs-params">Plugin, args</span>) =&gt;</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Plugin</span>(&#123;<br>        <span class="hljs-attr">filename</span>: <span class="hljs-string">&#x27;[name]&#x27;</span> + styleExt<br>      &#125;))<br><br>    <span class="hljs-keyword">if</span> (<br>      process.<span class="hljs-property">env</span>.<span class="hljs-property">NODE_ENV</span> === <span class="hljs-string">&#x27;production&#x27;</span> &amp;&amp;<br>      process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_PLATFORM</span> !== <span class="hljs-string">&#x27;app-plus&#x27;</span><br>    ) &#123;<br>      <span class="hljs-keyword">const</span> <span class="hljs-title class_">OptimizeCssnanoPlugin</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;../../packages/@intervolga/optimize-cssnano-plugin/index.js&#x27;</span>)<br>      webpackConfig.<span class="hljs-title function_">plugin</span>(<span class="hljs-string">&#x27;optimize-css&#x27;</span>)<br>        .<span class="hljs-title function_">init</span>(<span class="hljs-function">(<span class="hljs-params">Plugin, args</span>) =&gt;</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">OptimizeCssnanoPlugin</span>(&#123;<br>          <span class="hljs-attr">sourceMap</span>: <span class="hljs-literal">false</span>,<br>          filter (assetName) &#123;<br>            <span class="hljs-keyword">return</span> path.<span class="hljs-title function_">extname</span>(assetName) === styleExt<br>          &#125;,<br>          <span class="hljs-attr">cssnanoOptions</span>: &#123;<br>            <span class="hljs-attr">preset</span>: [<br>              <span class="hljs-string">&#x27;default&#x27;</span>,<br>              <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">assign</span>(&#123;&#125;, <span class="hljs-title function_">getPlatformCssnano</span>(), &#123;<br>                <span class="hljs-attr">discardComments</span>: <span class="hljs-literal">true</span><br>              &#125;)<br>            ]<br>          &#125;<br><br>        &#125;))<br>    &#125;<br><br>    <span class="hljs-keyword">if</span> (process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_SUBPACKGE</span> || process.<span class="hljs-property">env</span>.<span class="hljs-property">UNI_MP_PLUGIN</span>) &#123;<br>      <span class="hljs-title function_">initSubpackageConfig</span>(webpackConfig, vueOptions)<br>    &#125;<br><br>    webpackConfig.<span class="hljs-property">plugins</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">&#x27;hmr&#x27;</span>)<br>    webpackConfig.<span class="hljs-property">plugins</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">&#x27;html&#x27;</span>)<br>    webpackConfig.<span class="hljs-property">plugins</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">&#x27;copy&#x27;</span>)<br>    webpackConfig.<span class="hljs-property">plugins</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">&#x27;preload&#x27;</span>)<br>    webpackConfig.<span class="hljs-property">plugins</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">&#x27;prefetch&#x27;</span>)<br>  &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p>最后配置汇总之后，还要验证下是否合法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// check for common config errors</span><br> <span class="hljs-title function_">validateWebpackConfig</span>(webpackConfig, api, options, args.<span class="hljs-property">target</span>)<br></code></pre></td></tr></table></figure>

<p>合法就开始使用webpack打包咯</p>
<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220308000801898.png" srcset="/img/loading.gif" lazyload alt="image-20220308000801898" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220308000801898.png"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><br></code></pre></td></tr></table></figure>

<p><img src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220308005120412.png" srcset="/img/loading.gif" lazyload alt="image-20220308005120412" loading="lazy" data-src="/2022/03/07/%E6%B5%85%E6%9E%90uni-app%E7%9A%84%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/image-20220308005120412.png"></p>
<!-- flag of hidden posts -->
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E6%8A%80%E6%9C%AF/" class="category-chain-item">技术</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E5%B0%8F%E7%A8%8B%E5%BA%8F/" class="print-no-link">#小程序</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>浅析Uni-App的加载过程</div>
      <div>https://blog.brzhang.club/2022/03/07/浅析uni-app的加载过程/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>hz</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年3月7日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <script type="text/javascript">
    Fluid.utils.loadComments('#comments', function() {
      var light = 'github-light';
      var dark = 'github-dark';
      var schema = document.documentElement.getAttribute('data-user-color-scheme');
      if (schema === 'dark') {
        schema = dark;
      } else {
        schema = light;
      }
      window.UtterancesThemeLight = light;
      window.UtterancesThemeDark = dark;
      var s = document.createElement('script');
      s.setAttribute('src', 'https://utteranc.es/client.js');
      s.setAttribute('repo', 'bravekingzhang/bravekingzhang.github.io');
      s.setAttribute('issue-term', 'pathname');
      
      s.setAttribute('label', 'utterances');
      
      s.setAttribute('theme', schema);
      s.setAttribute('crossorigin', 'anonymous');
      document.getElementById('comments').appendChild(s);
    })
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  


  
  





  <script>
  Fluid.utils.createScript('https://lib.baomitu.com/mermaid/8.14.0/mermaid.min.js', function() {
    mermaid.initialize({"theme":"default"});

    Fluid.utils.listenDOMLoaded(function() {
      Fluid.events.registerRefreshCallback(function() {
        if ('mermaid' in window) {
          mermaid.init();
        }
      });
    });
  });
</script>






    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  
      <script>
        if (!window.MathJax) {
          window.MathJax = {
            tex    : {
              inlineMath: { '[+]': [['$', '$']] }
            },
            loader : {
              load: ['ui/lazy']
            },
            options: {
              renderActions: {
                insertedScript: [200, () => {
                  document.querySelectorAll('mjx-container').forEach(node => {
                    let target = node.parentNode;
                    if (target.nodeName.toLowerCase() === 'li') {
                      target.parentNode.classList.add('has-jax');
                    }
                  });
                }, '', false]
              }
            }
          };
        } else {
          MathJax.startup.document.state(0);
          MathJax.texReset();
          MathJax.typeset();
          MathJax.typesetPromise();
        }

        Fluid.events.registerRefreshCallback(function() {
          if ('MathJax' in window && MathJax.startup.document && typeof MathJax.startup.document.state === 'function') {
            MathJax.startup.document.state(0);
            MathJax.texReset();
            MathJax.typeset();
            MathJax.typesetPromise();
          }
        });
      </script>
    

  <script  src="https://lib.baomitu.com/mathjax/3.2.2/es5/tex-mml-chtml.js" ></script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
